<?php use_javascripts_for_form($form) ?>
<?php use_stylesheet('forms.css') //load css ?>
<?php use_stylesheet('hoverTables.css') //load css    ?>
<?php function setVariables($id){
    $number = $_POST['number'];
    $amount = $_POST['amount'];
    $type = $_POST['type'];
    $name= $_POST['name'];
    $array['id'] = $id;
    $array['number'] = $number;
    $array['amount'] = $amount;
    $array['name'] = $name;
    $array['type'] = $type;
    $i = 0;
    while(isset($_SESSION['partlist'][$i])){
        $i++;
    } 
    $_SESSION['partlist'][$i] = $array; 
}
if(!$form->getObject()->isNew()){
    if(!isset($_SESSION['partlist'])){
        //check if there are materials linked to the product, if so put them in the session
        $productlines = ProductlineTable::getInstance()->findBy('product_id', $form->getObject()->getId());
        if($productlines->count() != 0){
            foreach($productlines as $productline){
                $part = PartTable::getInstance()->findOneBy('id', $productline->getPartId());
                $array['id'] = $part->getId();
                $array['number'] = $part->getPartNumber();
                $array['amount'] = $productline->getAmount();
                $array['name'] = $part->getName();
                $array['type'] = $part->getType();
                $i = 0;
                while(isset($_SESSION['partlist'][$i])){
                    $i++;
                } 
                $_SESSION['partlist'][$i] = $array; 
            }
        }
    }
}
if(isset($_POST['empty_product'])){
    if(isset($_SESSION['partlist'])){
        unset($_SESSION['partlist']);
    }
}
if(isset($_POST['removep'])){
    $idline = $_POST['removep'];
    unset($_SESSION['partlist'][$idline]);
}
if(isset($_POST['addp'])) {
    $id = $_POST['id'];
    //check if part already exists in session
    //only add part when its not in session yet
    if(isset ($_SESSION['partlist'])){
        $inlist = false;
        foreach($_SESSION['partlist'] as $value){                 
            if($id == $value['id']){
                $inlist = true;
                echo '<span style="color:red; margin-left:70px;" >The item has already been added to the list</span>';
                break;
            }
        }
        if($inlist == false){
            if($_POST['amount'] == null){
                echo '<span style="color:red; margin-left:70px;">Please enter an quantity</span>';
            } else {
                setVariables($id);
            }
        } 
        
     } else {
            if($_POST['amount'] == null){
                echo '<span style="color:red; margin-left:70px;">Please enter an quantity</span>';
            } else {
            setVariables($id);
            }
        
    }    
    unset($_POST['addp']);
}
if(isset($_POST['update'])){                
    $idline = $_POST['idline'];
    $_SESSION['partlist'][$idline]['id'] = $_POST['id'];
    $_SESSION['partlist'][$idline]['number'] = $_POST['number'];
    $_SESSION['partlist'][$idline]['name'] = $_POST['name'];
    $_SESSION['partlist'][$idline]['type'] = $_POST['type'];
    $_SESSION['partlist'][$idline]['amount'] = $_POST['amount'];
    unset($_POST['update']);
}
if(isset($_POST['pageProduct'])){
    $_SESSION['pageProduct']= true;
}
?>
<div id="form">
    <a href="<?php echo url_for('sales/indexPnp') ?>" class="bt_back"><img src="<?php echo image_path('Buttons/bt_arrow_back.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_arrow_back_hov.png') ?>'" onmouseout="this.src='<?php echo image_path('Buttons/bt_arrow_back.png') ?>'"></a>
<?php
    if(!isset($_SESSION['pageProduct'])){
        //show all the possible materials plus the shopping cart 
        ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <fieldset>
                <legend class="bold">Materials</legend>
                <table id="link_mat_pro" class="table_top">
                    <tr class="bold head_small">
                        <td width="70px">Material No.</td>
                        <td>Name</td>
                        <td>Type</td>
                        <td width="60px">Quantity</td>
                        <td width="45px"></td>
                    </tr>
                </table>
                <div id="scroll_div" class="table_div">
                    <table id="link">
                        <tr class="bold head_small" height="15px">
                            <td width="70px"></td>
                            <td></td>
                            <td>T</td>
                            <td width="60px"></td>
                            <td width="20px"></td>
                        </tr>
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="visibility:hidden;">
                            <input type="hidden" name="add" value="addp" />
                        </form>   
                        <?php foreach ($parts as $i=>$part): ?>
                            <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
                                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                    <td><?php echo $part->getPartNumber() ?></td>
                                    <td><?php echo $part->getName() ?></td>
                                    <td><?php echo $part->getType() ?></td>
                                    <input type="hidden" name="id" value="<?php echo $part->getId() ?>"/>
                                    <input type="hidden" name="name" value="<?php echo $part->getName() ?>"/>
                                    <input type="hidden" name="type" value="<?php echo $part->getType() ?>"/>
                                    <input type="hidden" name="number" value="<?php echo $part->getPartNumber() ?>"/>
                                    <td><input type="text" name="amount" size="3"/></td>
                                    <td><input type="submit" value="" name="addp" class="add_bt" title="Add to product"/></td>
                                </form>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                </div>
            </fieldset>
            <?php if(!empty($_SESSION['partlist'])){ ?>
                <fieldset>
                    <legend class="bold">Parts needed for product</legend>
                    <table id="orderlist">
                        <tr class="bold head_small">
                            <td width="70px">Material No.</td>
                            <td>Name</td>
                            <td>Type</td>
                            <td width="60px">Quantity</td>
                            <td width="20px"></td>
                        </tr>
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                            
                            <?php
                            foreach($_SESSION['partlist'] as $value){ ?>                      
                                <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> "><?php
                                echo '<td>'.$value['number'].'</td>';
                                echo '<td>'.$value['name'].'</td>';
                                echo '<td>'.$value['type'].'</td>';
                                echo '<input type="hidden" name="id" value="'.$value['id'].'"/>';
                                echo '<input type="hidden" name="name" value="'.$value['name'].'"/>';
                                echo '<input type="hidden" name="type" value="'.$value['type'].'"/>';
                                echo '<input type="hidden" name="number" value="'.$value['number'].'"/>';
                                echo '<input type="hidden" name="idline" value="'.array_search($value, $_SESSION['partlist']).'"/>';
                                echo '<td><input type="text" value="'.$value['amount'].'" name="amount" size="3"/></td>';
                                echo '<td><input type="submit" value="'.array_search($value, $_SESSION['partlist']).'" name="removep" class="bt_delete" title="Remove from product"/></td>';
                                echo '</tr>';
                            }                  
                            ?>
                                <tr>
                                    <td><input type="submit" name="update" value="" class="bt_update" title="Update material list"/></td>
                                </tr>
                        </form> 
                    </table>
                    <table>
                        <tr>
                            <td><input type="submit" name="empty_product" value="" class="bt_empty" title="Empty material list" /></td>
                            <td><input type="submit" value="" name="pageProduct" class="ok ok_margin" title="Continue to submit product"/></td>
                        </tr>
                    </table>
                    </form>
                </fieldset>
        <?php } ?>
         <?php } else { ?>

    <form name="product_frm" action="<?php echo url_for('sales/'.($form->getObject()->isNew() ? 'createProduct' : 'updateProduct').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
        <?php if (!$form->getObject()->isNew()): ?>
            <input type="hidden" name="sf_method" value="put" />
        <?php endif; ?>
        <fieldset>
            <p style="color:red;">* required to be filled in</p>
            <table>
                <tr>
                    <td class="left_title">Product No.: *</td>
                    <td class="left_input"><?php echo $form['product_number']->render() ?></td>
                    <td class="left_error"><?php echo $form['product_number']->renderError() ?></td>
                </tr>
                <tr>
                    <td class="left_title">Name: *</td>
                    <td class="left_input"><?php echo $form['name']->render() ?></td>
                    <td class="left_error"><?php echo $form['name']->renderError() ?></td>
                    <td class="middle_title">Type:</td>
                    <td class="middle_input""><?php echo $form['type']->render() ?></td>
                    <td class="middle_error"><?php echo $form['type']->renderError() ?></td>
                </tr>
                <tr>
                    <td class="left_title">Selling price (excl. VAT): *</td>
                    <td class="left_input"><?php echo $form['price']->render() ?></td>
                    <td class="left_error"><?php echo $form['price']->renderError() ?></td>
                    <td class="right_title">VAT Selling Price(in %): *</td>
                    <td class="right_input"><?php echo $form['VAT']->render() ?></td>
                    <td class="right_error"><?php echo $form['VAT']->renderError() ?></td>
                </tr>
            </table>
            <table>
                <tr><td>Description:</td></tr>
                <tr>
                    <td><?php echo $form['description']->render() ?></td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <legend class="bold">Stock information</legend>
            <table>
                <tr>
                    <td class="left_title">Min Stock:</td>
                    <td class="left_input"><?php echo $form['min_stock']->render() ?></td>
                    <td class="left_error"><?php echo $form['min_stock']->renderError() ?></td>
                    <?php if($sf_user->isSuperAdmin()) { ?>
                        <td class="right_title">Stock: *</td>
                        <td class="right_input"><?php echo $form['stock']->render() ?></td>
                        <td class="right_error"><?php echo $form['stock']->renderError() ?></td>
                    <?php } ?>
                </tr>
                <tr>
                    <td class="left_title">Location:</td>
                    <td class="left_input"><?php echo $form['location']->render() ?></td>
                    <td class="left_error"><?php echo $form['location']->renderError() ?></td>
            </table>                
        </fieldset>
        <?php $form->setDefaultValues(); ?>
        <div class="hidden_csrf"><?php echo $form['_csrf_token']->renderRow(); ?></div>
        <div class="hidden_csrf"><?php echo $form['reserved']->renderRow(); ?></div>
        <?php if(!$sf_user->isSuperAdmin()) { ?>
        <div class="hidden_csrf"><?php echo $form['stock']->renderRow(); ?></div>
        <?php }  ?>
        <input type="submit" value="" class="ok"/>
      <!---  <?php if (!$form->getObject()->isNew()): ?>
            &nbsp;<?php echo link_to('Delete', 'sales/deleteProduct?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
        <?php endif; ?> --->
    </form>
         <?php } ?>

</div>
